package com.framework.plm.sec.encoder;

import java.util.HashMap;
import java.util.Map;

import org.springframework.security.authentication.encoding.MessageDigestPasswordEncoder;

class temp {
	static int iLength = 0;
	static String ch = "";
}

public class PasswordUtil extends MessageDigestPasswordEncoder implements IPasswordEncoder {
	public static int A[] = { 31, -42, 53, -64, 75, -86, 97, -108 };
	public static String passwordKey = "password";
	public static String decodeKey = "decode";

	public String encodePassword(String rawPass, Object salt) {
		return PwdEncode(rawPass)[0];
	}

	public String getAlgorithm() {
		return super.getAlgorithm();
	}

	public boolean isPasswordValid(String encPass, String rawPass, Object salt) {
		String pass1 = "" + encPass;
		String pass2 = encodePassword(rawPass, salt);
		return pass1.equals(pass2);
		//return true;
	}

	public PasswordUtil(String algorithm) {
		super(algorithm);
	}

	public PasswordUtil() {
		super("MD5");
	}
	
	public String encode(String passwd) {
		if (passwd == null) {
			return null;
		} else {
			return PasswordUtil.PwdEncode(passwd)[0];
		}
	}

	public static String[] PwdEncode(String strsrc) {
		strsrc = strsrc.trim();
		String temp_src = "";
		String temp_flag = "";
		if (strsrc.equals(""))
			return new String[]{"",""};
		for (int i = 0; i < strsrc.length(); i++) {
			if (IsAsCII(strsrc.charAt(i), A[i])) {
				temp_src += (String.valueOf((char) Integer.parseInt(temp.ch)));
				temp_flag += String.valueOf(temp.iLength);
			}
		}
		return new String[]{temp_src, temp_flag.trim()};

	}

	//判断是否加密后是否落在ASCII为 33-126之间 ，并存入标志为和加密后的一个字符
	//参数一：ch1:加密前的每位字符  iLength:幅值
	public static boolean IsAsCII(char ch1, int iLength) {
		int iLevel = 0;
		int ch = 0;
		ch = ch1;
		do //至少执行一次
		{
			ch = ch + iLength;//n*iLength;
			if (ch < 0)
				ch += 256;
			if (ch > 256)
				ch -= 256;
			iLevel++;
		} while (ch < 33 || ch > 126);
		temp.iLength = iLevel; //存放循环次数
		temp.ch = String.valueOf(ch); //存放经过转换后的字符
		return true;
	}

	public static String PwdDecode(String strsrc, String strFlag) {
		strsrc = strsrc.trim();
		strFlag = strFlag.trim();
		String strResult = "";
		int tmpvalue = 0;
		if (strsrc.equals(""))
			return "";
		if (strsrc.length() != strFlag.length())
			return "长度不一样，该串不能解码！";
		for (int i = 0; i < strsrc.length(); i++) {
			tmpvalue = strsrc.charAt(i) - A[i]
					* (Integer.parseInt(String.valueOf(strFlag.charAt(i))));
			if (tmpvalue > 0) {
				tmpvalue = tmpvalue % 256;
			} else if (tmpvalue < 0) {
				tmpvalue = 256 - ((Math.abs(tmpvalue)) % 256);
			}
			strResult += (String.valueOf((char) tmpvalue));
		}
		return strResult;
	}

	@SuppressWarnings("unchecked")
	public static Map generatePassword(){
		Map passwordMap = new HashMap();
		int srcPassword= (int) (Math.random()*(99999999-10000000+1)+10000000);
		String keys[] = PasswordUtil.PwdEncode(srcPassword+"");
		String passwordEncode = keys[0];
		String decode = keys[1];
		passwordMap.put(passwordKey, passwordEncode);
		passwordMap.put(decodeKey, decode);
		return passwordMap;
	}
	
	public static void main(String[] args) {
		String[] keys = PasswordUtil.PwdEncode("654321");
		String passwordEncode = keys[0];
		String decode = keys[1];
		System.out.println(passwordEncode+":"+decode);
		System.out.println(PasswordUtil.PwdDecode("Xdkve4", "151343"));
		
		String id="fjxmwlg@"+System.currentTimeMillis();
		String code=new SHAPasswordEncoder().encode(id);
		System.out.println(id);
		System.out.println(code);
	}
}
